/*
 * The MIT License (MIT)
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */

use napi::{
  bindgen_prelude::{ClassInstance, Function, FunctionRef},
  Env, JsFunction, JsObject, JsUnknown, Result,
};
use napi_derive::napi;

use crate::class::{Animal, ZeroStruct};

#[napi]
pub fn call0(callback: JsFunction) -> Result<u32> {
  callback.call0()
}

#[napi]
pub fn call1(callback: JsFunction, arg: u32) -> Result<u32> {
  callback.call1(arg)
}

#[napi]
pub fn call2(callback: JsFunction, arg1: u32, arg2: u32) -> Result<u32> {
  callback.call2(arg1, arg2)
}

// No way to pass this in arkts function
#[napi]
pub fn apply0(ctx: ClassInstance<Animal>, callback: JsFunction) -> Result<()> {
  callback.apply0(ctx)
}

// No way to pass this in arkts function
#[napi]
pub fn apply1(ctx: ClassInstance<Animal>, callback: JsFunction, name: String) -> Result<()> {
  callback.apply1(ctx, name)
}

#[napi]
pub fn call_function(cb: Function<(), u32>) -> Result<u32> {
  cb.call(())
}

#[napi]
pub fn call_function_with_arg(cb: Function<(u32, u32), u32>, arg0: u32, arg1: u32) -> Result<u32> {
  cb.call((arg0, arg1))
}

#[napi(ts_return_type = "Promise<void>")]
pub fn create_reference_on_function(env: Env, cb: Function<(), ()>) -> Result<JsObject> {
  let reference = cb.create_ref()?;
  env.execute_tokio_future(
    async {
      tokio::time::sleep(std::time::Duration::from_millis(100)).await;
      Ok(())
    },
    move |env, _| {
      let cb = reference.borrow_back(env)?;
      cb.call(())?;
      Ok(())
    },
  )
}

#[napi]
pub fn call_function_with_arg_and_ctx(
  ctx: ClassInstance<Animal>,
  cb: Function<String, ()>,
  name: String,
) -> Result<()> {
  cb.apply(ctx, name)
}

#[napi]
pub fn reference_as_callback(
  env: Env,
  callback: FunctionRef<(u32, u32), u32>,
  arg0: u32,
  arg1: u32,
) -> Result<u32> {
  callback.borrow_back(&env)?.call((arg0, arg1))
}

#[napi]
pub fn zero_struct_ptr() -> usize {
  let obj = Box::new(ZeroStruct);
  let value_ref = Box::into_raw(obj);
  let value_ref_size = value_ref as usize;
  let _x = unsafe { Box::from_raw(value_ref) };

  value_ref_size
}

#[napi]
pub fn return_diff_type(t: u32, env: Env) -> Result<JsUnknown> {
  match t {
    0 => env.create_string("hello").map(|s| s.into_unknown()),
    1 => env.create_int32(12).map(|s| s.into_unknown()),
    2 => env.create_double(1.2).map(|s| s.into_unknown()),
    _ => env
      .create_string("sorry not support")
      .map(|s| s.into_unknown()),
  }
}
